<?php defined('SYSPATH') or die('No direct script access.');

    /**租车应用
     *User: lh
     *Email: xslt@idoiwill.com
     *Blog: http://www.idoiwill.com
     *DateTime: 2020/7/17 12:16
     */
    class Controller_Pc_Api_V2_Exposed extends Controller_Pc_Api_V2_Base
    {

        public function before(){
            parent::before();
        }

        /**
         * @return void
         */
        public function action_orderList(){
            try {
                $typeid = intval($this->request_data->typeid);
                $add_time1 = strtotime($this->request_data->add_time1);
                $add_time2 = strtotime($this->request_data->add_time2);
                $status = intval($this->request_data->status);
                $page_size = intval($this->request_data->page_size);
                $page = intval($this->request_data->page);

                if(!$typeid){
                    throw new Exception('参数typeid不能为空');
                }
                if(!$page || !$page_size){
                    throw new Exception('参数page和page_size不能为空');
                }
                if($page_size>20){
                    throw new Exception('page_size不能大于20');
                }
                $orderObj = DB::select()->from('member_order')
                    ->where("typeid not in(107) and pid=0");

                //产品类型
                if($typeid){
                    $orderObj->and_where('typeid', '=', $typeid);
                }
                //订单状态
                if (in_array($status,[0,1,2,3,4,5,6,7,8])) {
                    $orderObj->and_where('status', '=', $status);
                }
                //下单时间
                if($add_time1 && $add_time2){
                    $orderObj->and_where('addtime', '>=', $add_time1);
                    $orderObj->and_where('addtime', '<=', $add_time2);
                }
                $totalObj = clone $orderObj;
                $data = $orderObj->limit($page_size)
                    ->offset($page_size * ($page - 1))
                    ->order_by('id', 'desc')
                    ->execute()
                    ->as_array();

                $arr=[];
                foreach ($data as $k=>$v) {
                    $info = Model_Member_Order::info($v['ordersn']);
                    $arr[$k]['litpic']=Model_Api_V2_System::uniapp_img($v['litpic'], 258, 175);
                    $arr[$k]['addtime']=date('Y-m-d H:i', $v['addtime']);
                    $arr[$k]['is_commentable']=Model_Model::is_commentable($v['typeid']);
                    $arr[$k]['statusname']=Model_Member_Order::get_status_name($v['status'], $v['typeid']);
                    list($productname, $ticket_type_name) = explode(' -- ', $v['productname']);
                    $arr[$k]['productname'] = $productname;
                    //全额支付
                    $arr[$k]['totalprice'] = $info['totalprice'];
                    //支付方式
                    switch ($v['paytype']) {
                        case '1':
                            $arr[$k]['paytype'] = '全款支付';
                            break;
                        case '2':
                            $arr[$k]['paytype'] = '定金支付';
                            break;
                        default:
                            $arr[$k]['paytype'] = '线下支付';
                            break;
                    }
                    $arr[$k]['ordersn'] = $v['ordersn'];
                    $arr[$k]['member'] = DB::select('nickname')->from('member')->where('mid','=',$v['memberid'])->execute()->get('nickname');
                    $arr[$k]['linkman'] = $v['linkman'];
                    $arr[$k]['linktel'] = $v['linktel'];
                }

                $count=$totalObj->select(array(DB::expr('COUNT( `id`)'), 'total'))->as_object()->execute()->current()->total;
                $result = array(
                    'status' => true,
                    'data' =>[
                        'list' => $arr,
                        'count' => $count,
                    ]
                );
                $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
            }catch (Exception $e) {
                $result = array(
                    'status' => false,
                    'msg' => $e->getMessage(),
                );
                $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
            }
        }


        public function action_orderInfo(){
            try{
                $ordersn = trim($this->request_data->ordersn);
                if(!$ordersn){
                    throw new Exception('参数ordersn不能为空');
                }
                $id = DB::select()->from('member_order')->where('ordersn','=',$ordersn)->execute()->get('id');
                $info = Model_Api_V2_Order::get_detail($id);

                $data['ordersn'] = $info['ordersn'];
                $data['productname'] = $info['productname'];
                $data['litpic'] = $info['litpic'];
                $data['pay_price'] = $info['pay_price'];
                $data['usedate'] = $info['usedate'];
                $data['dingnum'] = $info['dingnum'];
                $data['childnum'] = $info['childnum'];
                $data['oldnum'] = $info['oldnum'];
                $data['suit_name'] = $info['suit_name'];
                $data['linkman'] = $info['linkman'];
                $data['linktel'] = $info['linktel'];
                $data['linkemail'] = $info['linkemail'];
                $data['linkidcard'] = $info['linkidcard'];
                $data['paytype_name'] = $info['paytype_name'];
                $data['statusname'] = $info['statusname'];
                $data['member_info'] = $info['member_info'];
                $data['tourer_list'] = $info['tourer_list'];
                $data['contract_content'] = $info['contract_content'];
                $data['receiver_address_infos'] = $info['receiver_address_infos']?json_decode($info['receiver_address_infos'], true):[];
                $result = array(
                    'status' => true,
                    'data' => $data,
                );
                $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
            }catch (Exception $e) {
                $result = array(
                    'status' => false,
                    'msg' => $e->getMessage(),
                );
                $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
            }
        }
    }